/*************************************************************************
    > File Name: client.cpp
    > Author: Lancer
    > Mail: 18826075768@163.com
************************************************************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define P_FIFO "/tmp/p_fifo"

using namespace std;

int main(int argc, char** argv){
    char cache[100];
    int fd;
    memset(cache, 0, sizeof(cache));

    if(access(P_FIFO, F_OK) == 0){
        execlp("rm", "-f", P_FIFO, NULL);
        printf("access.\n");
    }

    if(mkfifo(P_FIFO, 0777) < 0){
        printf("create named pipe failed.\n");
    }

    fd = open(P_FIFO, O_RDONLY | O_NONBLOCK);
    while(1){
        memset(cache, 0, sizeof(cache));
        if(read(fd, cache, 100) == 0){
            printf("no data!\n");
        }
        else{
            printf("got data: %s\n", cache);
        }
        sleep(5);
    }
    close(fd);
    return 0;
}